/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package argentracker.activeentity.property;

import java.util.Date;

import argentracker.activeentity.ActiveEntity;
import argentracker.activeentity.P;
import argentracker.activeentity.exceptions.ActiveEntityException;

/**
 * briefly description.
 * 
 * extense description.
 * 
 * @author matero (matero _at_ gmail _dot_ com)
 */
public class DateProperty<AE extends ActiveEntity<AE>> extends P<Date, AE> {

    private final Long traductedDefaultValue;

    /**
     * Constructs an Date property with no name and <code>null</code> as default value.
     */
    public DateProperty() {
        this(NO_NAME, null);
    }

    /**
     * Construts an Date property defining its name, and setting <code>null</code> as default value.
     * 
     * @param propertyName
     */
    public DateProperty(String propertyName) {
        this(propertyName, null);
    }

    /**
     * Constructs an Date property defining its name and default value.
     * 
     * @param propertyName
     */
    public DateProperty(String propertyName, Date propertyDefaultValue) {
        super(Date.class, propertyName, propertyDefaultValue);
        traductedDefaultValue = propertyDefaultValue == null ? null : propertyDefaultValue.getTime();
    }

    @Override
    public Date interpret(Object value) {
        if (value instanceof Date)
            return (Date) value;
        if (value instanceof Number) {
            Number number = (Number) value;
            return new Date(number.longValue());
        }

        throw new ActiveEntityException("illegal value class, expected one of [" + getType().getName()
                + ", Number] received [" + value.getClass().getName() + "].");

    }

    @Override
    public Object traduct(Date value) {
        return value == null ? traductedDefaultValue : Long.valueOf(value.getTime());
    }
}
